package com.caigou.modules.query.ui.longest

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.screenCapture
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.utils.MediaUtils
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.query.adpter.LongestCodeItemAdapter
import com.caigou.modules.query.bean.LongestCodeItemBean
import com.caigou.modules.query.databinding.ActivityLongestCodeBinding
import com.caigou.modules.query.viewmodel.LongestCodeViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2022/01/04
 */
@Route(path = RouterPath.Query.PAGE_LONGEST_CODE)
@AndroidEntryPoint
class LongestCodeActivity : BaseActivity() {

    private lateinit var binding: ActivityLongestCodeBinding
    private val longestCodeViewModel: LongestCodeViewModel by viewModels()
    private var tabType = 0
    private var name = "特码"

//    private lateinit var colName: Column<String>

    //    private val colCurrentNotOpen = Column<Int>("当前最久不开", "currentNotOpened")
//    private val colHistoryNotOpen = Column<Int>("历史最久不开", "historyNotOpened")
    private val mAdapter: LongestCodeItemAdapter by lazy {
        LongestCodeItemAdapter(mutableListOf())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLongestCodeBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.longestCodeLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)

        binding.longestCodeRecyclerView.run {
            layoutManager = LinearLayoutManager(this@LongestCodeActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@LongestCodeActivity))
        }

        binding.longestCodeRgTypeSelector.setOnCheckedChangeListener { _, checkedId ->
            tabType = when (checkedId) {
                binding.longestCodeBtnTeMa.id -> {
                    binding.longestCodeTvName.text = "特码"
                    0
                }
                binding.longestCodeBtnBoSe.id -> {
                    binding.longestCodeTvName.text = "波色"
                    1
                }
                binding.longestCodeBtnTouShu.id -> {
                    binding.longestCodeTvName.text = "头数"
                    2
                }
                binding.longestCodeBtnWeiShu.id -> {
                    binding.longestCodeTvName.text = "尾数"
                    3
                }
                binding.longestCodeBtnWuXing.id -> {
                    binding.longestCodeTvName.text = "五行"
                    4
                }
                binding.longestCodeBtnShengXiao.id -> {
                    binding.longestCodeTvName.text = "生肖"
                    5
                }
                binding.longestCodeBtnBanBo.id -> {
                    binding.longestCodeTvName.text = "半波"
                    6
                }
                binding.longestCodeBtnBanTou.id -> {
                    binding.longestCodeTvName.text = "半头"
                    7
                }
                binding.longestCodeBtnDuanShu.id -> {
                    binding.longestCodeTvName.text = "段数"
                    8
                }
                binding.longestCodeBtnHeShu.id -> {
                    binding.longestCodeTvName.text = "合数"
                    9
                }
                else -> {
                    binding.longestCodeTvName.text = "特码"
                    0
                }
            }
            fetchLongestCodeList()
        }
        binding.longestCodeTvBack.setOnClickListener {
            finish()
        }

        binding.longestCodeIvShare.setOnClickListener {
            val bitmap = binding.longestCodeLlContainer.screenCapture()
            MediaUtils.saveBitmap(this, bitmap)
            showToast("截图已保存到系统相册")
        }
        fetchLongestCodeList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(longestCodeViewModel.longestCodeListLiveData, ::handleGetLongestCodeList)
    }

    private fun handleGetLongestCodeList(result: Resource<MutableList<LongestCodeItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                result.data?.apply {
                    mAdapter.setList(this)
//                    binding.longestCodeTable.setData(this)
//                    binding.longestCodeRecyclerView.setTableData(TableData("", this, colName, colCurrentNotOpen, colHistoryNotOpen))
                }
            }
        }
    }

    private fun fetchLongestCodeList() {
        longestCodeViewModel.getLongestCodeList(tabType)
    }

}